<?php
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
class DocarioControllerCategory extends JController {
    
    function __construct() {
        parent::__construct();
        
        if (JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        $this->registerTask('addcategory', 'editcategory');
    }
    
    function editcategory() {
        JRequest::setVar('hidemainmenu', true);
        
        $view =& $this->getView('editcategory', 'html');
        $model = $this->getModel('category');
        $group = $this->getModel('group');
        
        $view->setModel($model, true);
        $view->setModel($group);
        
        $view->display();
    }
    
    function save() {
        $model = $this->getModel('category');
        if ($model->saveCategory()) {
            $msg = JText::_('CATEGORY_SAVED');
        } else $msg = JText::_('CATEGORY_SAVE_FAILED');
        
        $link = JRoute::_('index.php?option=com_docario&task=categories');
        $this->setRedirect($link, $msg);
    }
    
    function deleteCategory() {
        $cid = JRequest::getVar('cid', array(0));
        
        $model = $this->getModel('category');
        
        $valid = true;
        foreach ($cid as $id) {
            if (!$model->deleteCategory($id)) {
                $valid = false;
                break;
            }
        }
        
        if ($valid) $msg = JText::_('CATEGORY_DELETED');
        else $msg = JText::_('CATEGORY_DELETE_FAILED');
        
        $link = JRoute::_('index.php?option=com_docario&task=categories');
        $this->setRedirect($link, $msg);
    }
    
    function searchUsers() {
        $view =& $this->getView('catusertable', 'raw');
        
        $view->setModel($this->getModel('group'), true);
        
        $view->display();
    }
    
    function changereadperm() {
        $value = JRequest::getVar('value');
        $uid = JRequest::getVar('uid');
        $cid = JRequest::getVar('cid');
        
        $model = $this->getModel('category');
        $model->setPerm($cid, $uid, DocarioModelCategory::CATEGORY_ALLOW_READ, 
                        $value);
    }
    
    function changewriteperm() {
        $value = JRequest::getVar('value');
        $uid = JRequest::getVar('uid');
        $cid = JRequest::getVar('cid');
        
        $model = $this->getModel('category');
        $model->setPerm($cid, $uid, DocarioModelCategory::Category_ALLOW_WRITE, 
                        $value);
    }
    
    function publish() {
        $cid = JRequest::getVar('cid', array(0));
        
        $model = $this->getModel('category');
        
        $valid = true;
        foreach ($cid as $id) {
            if ($model->publish(DocarioModelCategory::CATEGORY_PUBLISHED, $id)) {
                $valid = false;
                break;
            } 
        }
        
        if ($valid) $msg = JText::_('CATEGORIES_PUBLISHED');
        else $msg = JText::_('CATEGORIES_PUBLISH_FAILED');
        
        $link = JRoute::_('index.php?option=com_docario&task=categories');
        $this->setRedirect($link, $msg);
    }
    
    function unpublish() {
        $cid = JRequest::getVar('cid', array(0));
        
        $model = $this->getModel('category');
        
        $valid = true;
        foreach ($cid as $id) {
            if ($model->publish(DocarioModelCategory::CATEGORY_UNPUBLISHED, $id)) {
                $valid = false;
                break;
            }
        }
        
        if ($valid) $msg = JText::_('CATEGORIES_UNPUBLISHED');
        else $msg = JText::_('CATEGORIES_UNPUBLISH_FAILED');
        
        $link = JRoute::_('index.php?option=com_docario&task=categories');
        $this->setRedirect($link, $msg);
    }
    
    function greadchange() {
        $cid = JRequest::getVar('cid');
        $gid = JRequest::getVar('gid');
        $value = JRequest::getVar('value');
        
        $model = $this->getModel('category');
        $model->setGroupPerm($cid, $gid, 
                             DocarioModelCategory::CATEGORY_ALLOW_READ, $value);
    }
    
    function gwritechange() {
        $cid = JRequest::getVar('cid');
        $gid = JRequest::getVar('gid');
        $value = JRequest::getVar('value');
        
        $model = $this->getModel('category');
        $model->setGroupPerm($cid, $gid,
                             DocarioModelCategory::CATEGORY_ALLOW_WRITE, $value);
    }
    
}
?>
